import os
from textwrap import fill
import tkinter as tk
from tkinter.filedialog import (askopenfilename, askdirectory)
import traceback
from turtle import bgcolor
#from tkinter import messagebox

from control.msg import req_dsd_file_singleTag, req_replace_ID3_chunk, req_reuse_ID3_chunk

def view_input_single_tag(fileName):
    root = tk.Tk()
    root.title('DFF Tag creater')

    singleTag = {}

    var = tk.StringVar()
    var.set("")

    artist = tk.StringVar()
    artist.set("Input artist here.")
    album = tk.StringVar()
    album.set("Input album here.")
    title = tk.StringVar()
    title.set("Input title here.")
    year = tk.StringVar()
    year.set("Input year here.")
    disc = tk.StringVar()
    disc.set("Input disc no. here.")
    track = tk.StringVar()
    track.set("Input track no. here.")

    frm = tk.Frame(root)
    frm.pack()
    frm_l = tk.Frame(frm)
    frm_r = tk.Frame(frm)
    frm_t = tk.Frame(frm)    
    frm_b = tk.Frame(frm)    

    frm_t.pack(side='top')
    frm_b.pack(side='bottom')

    frm_l.pack(side='left')
    frm_r.pack(side='right')

    tk.Label(frm_t, text='Input tags for DSDIFF file:').pack()
    tk.Label(frm_t, text=fileName).pack()    
    tk.Label(frm_t, text='------------------------------------').pack()    

    tk.Label(frm_l, text='Artist: ',  width=20, anchor='e').pack()    
    tk.Entry(frm_r, textvariable=artist, width=40).pack()        

    tk.Label(frm_l, text='Album: ',  width=20, anchor='e').pack()    
    tk.Entry(frm_r, textvariable=album, width=40).pack()        
    
    tk.Label(frm_l, text='Year: ',  width=20, anchor='e').pack()    
    tk.Entry(frm_r, textvariable=year, width=40).pack()        

    tk.Label(frm_l, text='Title: ',  width=20, anchor='e').pack()    
    tk.Entry(frm_r, textvariable=title, width=40).pack()        

    tk.Label(frm_l, text='Track: ',  width=20, anchor='e').pack()    
    tk.Entry(frm_r, textvariable=track, width=40).pack()        

    def hit_Cancle():     
        var.set("Cancle")
        root.destroy()

    def hit_OK():     
        if(artist.get()!="" and title.get()!="" and artist.get()!=""):
            var.set("Ok")
            root.destroy()

    tk.Label(frm_b, text='------------------------------------').pack()    
    tk.Button(frm_b, text = "Cancle", width=20, height = 2, command=hit_Cancle).pack()
    tk.Button(frm_b, text = "OK", width=20, height = 2, command=hit_OK).pack()

    root.mainloop()
    singleTag[b'TALB'] = album.get()
    singleTag[b'TPE1'] = artist.get()
    singleTag[b'TIT2'] = title.get()
    singleTag[b'TRCK'] = track.get()
    singleTag[b'TYER'] = year.get()

    return var.get(), singleTag

def view_input_tag():
    root = tk.Tk()
    root.title('DFF Tag creater')

    var = tk.StringVar()
    var.set("")

    artist = tk.StringVar()
    artist.set("Input artist here.")
    album = tk.StringVar()
    album.set("Input album here.")
    title = tk.StringVar()
    title.set("Input title here.")

    frm = tk.Frame(root)
    frm.pack()
    frm_l = tk.Frame(frm)
    frm_r = tk.Frame(frm)
    frm_t = tk.Frame(frm)    
    frm_b = tk.Frame(frm)    

    frm_t.pack(side='top')
    frm_b.pack(side='bottom')

    frm_l.pack(side='left')
    frm_r.pack(side='right')

    tk.Label(frm_t, text='Input tags for DSDIFF file:').pack()
    tk.Label(frm_t, text='------------------------------------').pack()    

    tk.Label(frm_l, text='Artist: ',  width=20, anchor='e').pack()    
    tk.Entry(frm_r, textvariable=artist, width=40).pack()        

    tk.Label(frm_l, text='album: ',  width=20, anchor='e').pack()    
    tk.Entry(frm_r, textvariable=album, width=40).pack()        
    
    tk.Label(frm_l, text='Year: ',  width=20, anchor='e').pack()    
    tk.Entry(frm_r, textvariable=title, width=40).pack()        

    tk.Label(frm_l, text='Disc: ',  width=20, anchor='e').pack()    
    tk.Entry(frm_r, textvariable=title, width=40).pack()        

    tk.Label(frm_l, text='Title: ',  width=20, anchor='e').pack()    
    tk.Entry(frm_r, textvariable=title, width=40).pack()        

    tk.Label(frm_l, text='Track: ',  width=20, anchor='e').pack()    
    tk.Entry(frm_r, textvariable=title, width=40).pack()        

    def hit_Cancle():     
        var.set("Cancle")
        root.destroy()

    def hit_OK():     
        if(artist.get()!="" and title.get()!="" and artist.get()!=""):
            var.set("Ok")
            root.destroy()

    tk.Label(frm_b, text='------------------------------------').pack()    
    tk.Button(frm_b, text = "Cancle", width=20, height = 2, command=hit_Cancle).pack()
    tk.Button(frm_b, text = "OK", width=20, height = 2, command=hit_OK).pack()

    root.mainloop()
    return var.get(), album.get(), artist.get(), title.get()

def view_tag(album, artist, title ):


    root = tk.Tk()
    root.title('DFF Tag reader')

    var = tk.StringVar()
    var.set("")

#    root.geometry("300x200+630+80")

    frm = tk.Frame(root)
    frm.pack()
    frm_l = tk.Frame(frm)
    frm_r = tk.Frame(frm)
    frm_t = tk.Frame(frm)    
    frm_b = tk.Frame(frm)    

    frm_t.pack(side='top')
    frm_b.pack(side='bottom')

    frm_l.pack(side='left')
    frm_r.pack(side='right')

    tk.Label(frm_r, text='The tags in ID3tag from DSDIFF file:',  width=30, anchor='w').pack()
    tk.Label(frm_r, text='------------------------------------').pack()    
    tk.Label(frm_r, text='Artist: '+artist,  anchor='w').pack()
    tk.Label(frm_r, text='Album: '+album,  anchor='w').pack()    
    tk.Label(frm_r, text='Title: '+title,  anchor='w').pack()    
    tk.Label(frm_r, text='------------------------------------').pack()    

    def hit_Cancle():     
        var.set("Cancle")
        root.destroy()

    def hit_OK():     
        var.set("Ok")
        root.destroy()

    def hit_New():     
        var.set("New")
        root.destroy()

    tk.Button(frm_r, text = "Cancle", width=20, height = 2, command=hit_Cancle).pack()
    tk.Button(frm_r, text = "OK, use these tags", width=20, height = 2, command=hit_OK).pack()
    tk.Button(frm_r, text = "NOK, need new tags", width=20, height = 2, command=hit_New).pack()

    root.mainloop()
    return var.get()    


def view_path_selector():


    root = tk.Tk()
    root.title('DFF Path Selector')

    varPath = tk.StringVar()
    varPath.set("")
    varOutPath = tk.StringVar()
    varOutPath.set("")

    varAllFiles = tk.IntVar()
    varOutputType = tk.IntVar()

    frm = tk.Frame(root)
    frm.pack()
    frm_l = tk.Frame(frm)
    frm_r = tk.Frame(frm)
    frm_t = tk.Frame(frm)    
    frm_b = tk.Frame(frm)    

    frm_t.pack(side='top')
    frm_b.pack(side='bottom')

    frm_l.pack(side='left')
    frm_r.pack(side='right')

    def hit_ChoosePath():     
        path = askopenfilename(filetypes=[('DSDIFF file','*.dff')])
        varPath.set(path)
        if (varOutputType.get() == 0):
            inFileName = path
            outpath,  inFileName = os.path.split(inFileName)
            varOutPath.set(outpath)

    def hit_ChooseOutPath():    
        path = ""
        if (varOutputType.get() == 1):
            path = askdirectory()
        else:
            inFileName = varPath.get()
            path,  inFileName = os.path.split(inFileName)

        varOutPath.set(path)

    #tk.Label(frm_l, text='DSDIFF file path:').pack()
    
    tk.Label(frm_l, text='Input DSDIFF files:', width=25, anchor='w').pack()            
    tk.Button(frm_l, text = "Choose input files", \
        width=20, height = 2, command=hit_ChoosePath).pack()
    tk.Checkbutton(frm_l, text = "All files under the path", \
        variable = varAllFiles, onvalue=1, offvalue = 0).pack()
    tk.Checkbutton(frm_l, text = "Ouput files under the same path", \
        variable = varOutputType, onvalue=0, offvalue =1, command=hit_ChooseOutPath).pack()

    tk.Label(frm_l, text='------------------------------------').pack()            
    tk.Label(frm_l, text='Your input path is:',  width=25, anchor='w').pack()                
    tk.Label(frm_l, textvariable= varPath).pack()
    tk.Label(frm_l, text='------------------------------------').pack()                
    tk.Label(frm_l, text='Your output path is:',  width=25, anchor='w').pack()                
    tk.Label(frm_l, textvariable= varOutPath).pack()
    tk.Label(frm_l, text='------------------------------------').pack()                

    def hit_Cancle():     
        varPath.set("")
        root.destroy()

    def hit_OK():
        if varPath.get()!="":
            root.destroy()
    
    tk.Button(frm_l, text = "Cancle", width=20, height = 2, command=hit_Cancle).pack()
    tk.Button(frm_l, text = "OK", width=20, height = 2, command=hit_OK).pack()

    root.mainloop()
    return varPath.get(), varOutPath.get(), varAllFiles.get()


def view_selector():

    root = tk.Tk()
    root.title('DSDIFF tag editor')

    varChoice = tk.StringVar()
    varChoice.set("")

    frm = tk.Frame(root)
    frm.pack()
    frm_l = tk.Frame(frm)
    frm_r = tk.Frame(frm)
    frm_t = tk.Frame(frm)    
    frm_b = tk.Frame(frm)    

    frm_t.pack(side='top')
    frm_l.pack(side='left')
    frm_r.pack(side='right')
    frm_b.pack(side='bottom')

    def hit_dir():    
        varChoice.set("dir")
#        varPath.set(askopenfilename(filetypes=[('DSDIFF file','*.dff')]))
        root.destroy()        

    def hit_file():     
        varChoice.set("file")
#        varFile.set(askopenfilename(filetypes=[('DSDIFF file','*.dff')]))
        root.destroy()

    def hit_exit():     
        varChoice.set("")
        root.destroy()

    tk.Button(frm_l, text = "Move tag from ID3 Chunk to Prop Chunk", width=30, height = 2, command=hit_dir).pack()
    tk.Button(frm_b, text = "Modify tag of single File", width=30, height = 2, command=hit_file).pack()
    tk.Button(frm_r, text = "Exit", width=20, height = 2, command=hit_exit).pack()

    root.mainloop()
    return varChoice.get()

def view_file_editor():

    root = tk.Tk()
    root.title('DSDIFF File Tag Editor')

    varFilename = tk.StringVar()
    varAlbum = tk.StringVar()
    varArtist = tk.StringVar()
    varYear = tk.StringVar() 
    varTitle = tk.StringVar() 
    varTrack = tk.StringVar() 

    varDestOption = tk.IntVar()
    varDestOption.set(0)

    varDestFile = tk.StringVar()
    varDestFile.set("")

    var = tk.StringVar()
    var.set("")

    root.geometry('{}x{}'.format(460, 750))

    def update_albuminfo():
        singleTag = req_dsd_file_singleTag(varFilename.get())

        album = (singleTag[b'TALB'])
        artist = (singleTag[b'TPE1'])
        year = (singleTag[b'TYER'])        
        title = (singleTag[b'TPE1'])        
        track = (singleTag[b'TRCK'])    

        if(album != ""):
            varAlbum.set(album[1:].decode('utf-8'))
        else:
            varAlbum.set("Null")
        if(artist != ""):
            varArtist.set(artist[1:].decode('utf-8'))
        else:
            varArtist.set("Null")
        if(year != ""):
            varYear.set(year[1:].decode('utf-8'))
        else:
            varYear.set("Null")

    def update_trackinfo():
        singleTag = req_dsd_file_singleTag(varFilename.get())

        album = (singleTag[b'TALB'])
        artist = (singleTag[b'TPE1'])
        year = (singleTag[b'TYER'])        
        title = (singleTag[b'TIT2'])        
        track = (singleTag[b'TRCK'])    

        if(title != ""):
            varTitle.set(title[1:].decode('utf-8'))
        else:
            varTitle.set("Null")
        if(track != ""):
            varTrack.set(track[1:].decode('utf-8'))
        else:
            varTrack.set("Null")

    def dest_callback(*args):
        updateDest()

    varTitle.trace("w", dest_callback)
    varTrack.trace("w", dest_callback)

    def updateDest():
        inFileName = varFilename.get()
        if (inFileName != ""):
            pathName, inFileName = os.path.split(inFileName)
            base_name = os.path.splitext(inFileName)[0]
            file_extension = os.path.splitext(inFileName)[1]

            if(varDestOption.get() == 0):
                outFileName = os.path.join(pathName, base_name+"_out"+file_extension)
                varDestFile.set(outFileName)
            else:
                trackno = (varTrack.get()).split("/",1)
                base_name = trackno[0]                    
                outFileName = os.path.join(pathName, base_name+" - "+varTitle.get()+file_extension)                
                varDestFile.set(outFileName)

    def hit_select_file():
        varFilename.set(askopenfilename(filetypes=[('DSDIFF file','*.dff')]))  
        updateDest()              
#        update_albuminfo()
#        update_trackinfo()

    def hit_reload_album():
        update_albuminfo()

    def hit_reload_track():
        update_trackinfo()

    tk.Frame(root, width=440, height=120, highlightbackground="black", highlightthickness=1, bd=1).grid(row=0, column=0, columnspan=2, rowspan=3, sticky='W', padx=5, pady=5)
    tk.Label(root, text='File Info: ', width=30, anchor='w').grid(row=0, column=0, columnspan=2, padx=15, sticky='W')
    tk.Label(root, text='Filename: ', width=10, anchor='e').grid(row=1, column=0, columnspan=1, padx=6, sticky='W')
    tk.Entry(root, textvariable=varFilename, width=30).grid(row=1, column=1, columnspan=1, padx=15, sticky='W')
    tk.Button(root, text = "选择文件...", width=20, command=hit_select_file).grid(row=2, column=0, columnspan=2, rowspan=1, padx=25, sticky='W')

    tk.Frame(root, width=440, height=200, highlightbackground="black", highlightthickness=1, bd=1).grid(row=3, column=0, columnspan=2, rowspan=5, sticky='W', padx=5, pady=5)
    tk.Label(root, text='Album Info: ', width=30, anchor='w').grid(row=3, column=0, columnspan=2, padx=15, sticky='W')
    tk.Label(root, text='Album: ', width=10, anchor='e').grid(row=4, column=0, columnspan=1, padx=6, sticky='W')    
    tk.Entry(root, textvariable=varAlbum, width=30).grid(row=4, column=1, columnspan=1, padx=15, sticky='W')
    tk.Label(root, text='Artist: ', width=10, anchor='e').grid(row=5, column=0, columnspan=1, padx=6, sticky='W')    
    tk.Entry(root, textvariable=varArtist, width=30).grid(row=5, column=1, columnspan=1, padx=15, sticky='W')
    tk.Label(root, text='Year: ', width=10, anchor='e').grid(row=6, column=0, columnspan=1, padx=6, sticky='W')    
    tk.Entry(root, textvariable=varYear, width=30).grid(row=6, column=1, columnspan=1, padx=15, sticky='W')
    tk.Button(root, text = "从文件读取", width=20, command=hit_reload_album).grid(row=7, column=0, columnspan=2, rowspan=1, padx=25, sticky='W')

    tk.Frame(root, width=440, height=160, highlightbackground="black", highlightthickness=1, bd=1).grid(row=8, column=0, columnspan=2, rowspan=4, sticky='W', padx=5, pady=5)
    tk.Label(root, text='Track Info: ', width=30, anchor='w').grid(row=8, column=0, columnspan=2, padx=15, sticky='W')
    tk.Label(root, text='Title: ', width=10, anchor='e').grid(row=9, column=0, columnspan=1, padx=6, sticky='W')
    tk.Entry(root, textvariable=varTitle, width=30).grid(row=9, column=1, columnspan=1, padx=15, sticky='W')
    tk.Label(root, text='Track: ', width=10, anchor='e').grid(row=10, column=0, columnspan=1, padx=6, sticky='W')
    tk.Entry(root, textvariable=varTrack, width=30).grid(row=10, column=1, columnspan=1, padx=15, sticky='W')
    tk.Button(root, text = "从文件读取", width=20, command=hit_reload_track).grid(row=11, column=0, columnspan=2, rowspan=1, padx=25, sticky='W')


    tk.Frame(root, width=440, height=180, highlightbackground="black", highlightthickness=1, bd=1).grid(row=12, column=0, columnspan=2, rowspan=4, sticky='W', padx=5, pady=5)
    tk.Label(root, text='目的文件: ', width=30, anchor='w').grid(row=12, column=0, columnspan=2, padx=15, sticky='W')
    tk.Radiobutton(root, text='输入文件名+_out',variable=varDestOption, value=0, command=updateDest, anchor='e').grid(row=13, column=0, columnspan=2, padx=6, sticky='W')
    tk.Radiobutton(root, text='Track+Title', variable=varDestOption, value=1, command=updateDest, anchor='e').grid(row=14, column=0, columnspan=2, padx=6, sticky='W')    
    tk.Entry(root, textvariable=varDestFile, width=50).grid(row=15, column=0, columnspan=2, padx=15)

    def hit_Exit():     
#        var.set("Cancle")
        root.destroy()

    def hit_Apply(): 
        singleTag = {}    
        singleTag[b'TALB'] = varAlbum.get()
        singleTag[b'TPE1'] = varArtist.get()
        singleTag[b'TYER'] = varYear.get()
        singleTag[b'TIT2'] = varTitle.get()
        singleTag[b'TRCK'] = varTrack.get()

        buttonApply.config(state=tk.DISABLED)
        req_replace_ID3_chunk(varFilename.get(), varDestFile.get(), singleTag)        
        buttonApply.config(state=tk.NORMAL)        
#        var.set("Ok")
#        root.destroy()


    buttonApply = tk.Button(root, text = "应用", width=6, command=hit_Apply)
    buttonApply.grid(row=16, column=0, columnspan=1, rowspan=1, pady=10, sticky='E')
    tk.Button(root, text = "退出", width=6, command=hit_Exit).grid(row=16, column=1, columnspan=1, rowspan=1, pady=10)

    root.mainloop()
#    return varPath.get(), varOutPath.get(), varAllFiles.get()

def view_dir_editor():

    root = tk.Tk()
    root.title('DSDIFF Dir Tag Editor')

    varFilename = tk.StringVar()
    varAlbum = tk.StringVar()
    varArtist = tk.StringVar()
    varYear = tk.StringVar() 
    varTitle = tk.StringVar() 
    varTrack = tk.StringVar() 

    varDestOption = tk.IntVar()
    varDestOption.set(0)

    varDestPath = tk.StringVar()
    varDestPath.set("")

    varAllFiles = tk.IntVar()
    varAllFiles.set(1)

    var = tk.StringVar()
    var.set("")

    root.geometry('{}x{}'.format(460, 620))

    def update_albuminfo():
        singleTag = req_dsd_file_singleTag(varFilename.get())

        album = (singleTag[b'TALB'])
        artist = (singleTag[b'TPE1'])
        year = (singleTag[b'TYER'])        
        title = (singleTag[b'TPE1'])        
        track = (singleTag[b'TRCK'])    

        if(album != ""):
            varAlbum.set(album[1:].decode('utf-8'))
        else:
            varAlbum.set("Null")
        if(artist != ""):
            varArtist.set(artist[1:].decode('utf-8'))
        else:
            varArtist.set("Null")
        if(year != ""):
            varYear.set(year[1:].decode('utf-8'))
        else:
            varYear.set("Null")

    def update_trackinfo():
        singleTag = req_dsd_file_singleTag(varFilename.get())

        album = (singleTag[b'TALB'])
        artist = (singleTag[b'TPE1'])
        year = (singleTag[b'TYER'])        
        title = (singleTag[b'TIT2'])        
        track = (singleTag[b'TRCK'])    

        if(title != ""):
            varTitle.set(title[1:].decode('utf-8'))
        else:
            varTitle.set("Null")
        if(track != ""):
            varTrack.set(track[1:].decode('utf-8'))
        else:
            varTrack.set("Null")

#    def dest_callback(*args):
#        updateDest()

#    varTitle.trace("w", dest_callback)
#    varTrack.trace("w", dest_callback)

#    def updateDest():
#        inFileName = varFilename.get()
#        if (inFileName != ""):
#            pathName, inFileName = os.path.split(inFileName)
#            base_name = os.path.splitext(inFileName)[0]
#            file_extension = os.path.splitext(inFileName)[1]

#            if(varDestOption.get() == 0):
#                outFileName = os.path.join(pathName, base_name+"_out"+file_extension)
#                varDestFile.set(outFileName)
#            else:
#                trackno = (varTrack.get()).split("/",1)
#                base_name = trackno[0]                    
#                outFileName = os.path.join(pathName, base_name+" - "+varTitle.get()+file_extension)                
#                varDestFile.set(outFileName)

    def hit_select_file():
        varFilename.set(askopenfilename(filetypes=[('DSDIFF file','*.dff')]))  
        update_albuminfo()
#        update_trackinfo()

    def hit_target_path():
        varDestPath.set(askdirectory())  
#        updateDest()              
#        update_albuminfo()
#        update_trackinfo()

    def hit_reload_album():
        update_albuminfo()

    def hit_reload_track():
        update_trackinfo()

    #文件
    tk.Frame(root, width=440, height=120, highlightbackground="black", highlightthickness=1, bd=1).grid(row=0, column=0, columnspan=2, rowspan=3, sticky='W', padx=5, pady=5)
    tk.Label(root, text='File Info: ', width=30, anchor='w').grid(row=0, column=0, columnspan=2, padx=15, sticky='W')
    tk.Label(root, text='Filename: ', width=10, anchor='e').grid(row=1, column=0, columnspan=1, padx=6, sticky='W')
    tk.Entry(root, textvariable=varFilename, width=30).grid(row=1, column=1, columnspan=1, padx=15, sticky='W')
    tk.Button(root, text = "选择文件...", width=20, command=hit_select_file).grid(row=2, column=0, columnspan=2, rowspan=1, padx=25, sticky='W')

    #album
    tk.Frame(root, width=440, height=240, highlightbackground="black", highlightthickness=1, bd=1).grid(row=3, column=0, columnspan=2, rowspan=6, sticky='W', padx=5, pady=5)
    tk.Label(root, text='Album Info: ', width=30, anchor='w').grid(row=3, column=0, columnspan=2, padx=15, sticky='W')
    tk.Label(root, text='Album: ', width=10, anchor='e').grid(row=4, column=0, columnspan=1, padx=6, sticky='W')    
    tk.Entry(root, textvariable=varAlbum, width=30).grid(row=4, column=1, columnspan=1, padx=15, sticky='W')
    tk.Label(root, text='Artist: ', width=10, anchor='e').grid(row=5, column=0, columnspan=1, padx=6, sticky='W')    
    tk.Entry(root, textvariable=varArtist, width=30).grid(row=5, column=1, columnspan=1, padx=15, sticky='W')
    tk.Label(root, text='Year: ', width=10, anchor='e').grid(row=6, column=0, columnspan=1, padx=6, sticky='W')    
    tk.Entry(root, textvariable=varYear, width=30).grid(row=6, column=1, columnspan=1, padx=15, sticky='W')
    tk.Button(root, text = "从文件读取", width=20, command=hit_reload_album).grid(row=7, column=0, columnspan=2, rowspan=1, padx=25, sticky='W')

    tk.Checkbutton(root, text = "应用到同目录下所有文件", variable = varAllFiles, onvalue=1, offvalue = 0).grid(row=8, column=0, columnspan=2, rowspan=1, padx=25, sticky='W')

    #track
    #tk.Frame(root, width=440, height=160, highlightbackground="black", highlightthickness=1, bd=1).grid(row=8, column=0, columnspan=2, rowspan=4, sticky='W', padx=5, pady=5)
    #tk.Label(root, text='Track Info: ', width=30, anchor='w').grid(row=8, column=0, columnspan=2, padx=15, sticky='W')
    #tk.Label(root, text='Title: ', width=10, anchor='e').grid(row=9, column=0, columnspan=1, padx=6, sticky='W')
    #tk.Entry(root, textvariable=varTitle, width=30).grid(row=9, column=1, columnspan=1, padx=15, sticky='W')
    #tk.Label(root, text='Track: ', width=10, anchor='e').grid(row=10, column=0, columnspan=1, padx=6, sticky='W')
    #tk.Entry(root, textvariable=varTrack, width=30).grid(row=10, column=1, columnspan=1, padx=15, sticky='W')
    #tk.Button(root, text = "从文件读取", width=20, command=hit_reload_track).grid(row=11, column=0, columnspan=2, rowspan=1, padx=25, sticky='W')


    tk.Frame(root, width=440, height=160, highlightbackground="black", highlightthickness=1, bd=1).grid(row=9, column=0, columnspan=2, rowspan=4, sticky='W', padx=5, pady=5)
    tk.Label(root, text='目的目录: ', width=30, anchor='w').grid(row=10, column=0, columnspan=2, padx=15, sticky='W')
    #tk.Radiobutton(root, text='输入文件名+_out',variable=varDestOption, value=0, command=updateDest, anchor='e').grid(row=13, column=0, columnspan=2, padx=6, sticky='W')
    #tk.Radiobutton(root, text='Track+Title', variable=varDestOption, value=1, command=updateDest, anchor='e').grid(row=14, column=0, columnspan=2, padx=6, sticky='W')    
    tk.Entry(root, textvariable=varDestPath, width=50).grid(row=11, column=0, columnspan=2, padx=15)
    tk.Button(root, text = "选择目的目录...", width=20, command=hit_target_path).grid(row=12, column=0, columnspan=2, rowspan=1, padx=25, sticky='W')

    def hit_Exit():     
#        var.set("Cancle")
        root.destroy()

    def hit_Apply(): 

        buttonApply.config(state=tk.DISABLED)

        inFileName = varFilename.get()
        outPath = varDestPath.get()

        if (inFileName != ""):
            pathName, inFileName = os.path.split(inFileName)
            base_name = os.path.splitext(inFileName)[0]
            file_extension = os.path.splitext(inFileName)[1]

            if(outPath==""):
                outPath=pathName

            if(varAllFiles.get()==1):
                with os.scandir(pathName) as it:
                    for i in it:
                        if i.name.endswith(".dff"):
                            base_name = os.path.splitext(i.name)[0]
                            file_extension = os.path.splitext(i.name)[1]
                            inFile = os.path.join(pathName, i.name)
                            outFile = os.path.join(outPath, base_name+"_out"+file_extension)
                        
                        req_reuse_ID3_chunk(inFile, outFile)        

        buttonApply.config(state=tk.NORMAL)        
#        var.set("Ok")
#        root.destroy()


    buttonApply = tk.Button(root, text = "应用", width=6, command=hit_Apply)
    buttonApply.grid(row=16, column=0, columnspan=1, rowspan=1, pady=10, sticky='E')
    tk.Button(root, text = "退出", width=6, command=hit_Exit).grid(row=16, column=1, columnspan=1, rowspan=1, pady=10)

    root.mainloop()
#    return varPath.get(), varOutPath.get(), varAllFiles.get()
